package cn.taokuaidi.wangwang.client.reader;

import java.util.ArrayList;
import java.util.List;

import cn.taokuaidi.wangwang.client.model.ExpressDO;
import cn.taokuaidi.wangwang.client.model.OrderDO;
import cn.taokuaidi.wangwang.client.model.StatusInfo;
import cn.taokuaidi.wangwang.client.util.JSONUtil;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;

public class OrderReader<C> implements DataReader<C, ListLoadResult<ModelData>> {

    public PagingLoadResult read(C loadConfig, Object data) {
        JSONObject json = (JSONObject) JSONParser.parse((String) data);
        // 获得记录条数
        JSONNumber countObject = json.get("count").isNumber();
        int count = (int) countObject.doubleValue();
        // 获得偏移量
        JSONNumber offsetObject = json.get("offset").isNumber();
        int offset = (int) offsetObject.doubleValue();
        // 获得每条记录的order对象
        JSONArray root = json.get("items").isArray();
        int size = root.size();
        List<ModelData> records = new ArrayList<ModelData>();
        for (int i = 0; i < size; i++) {
            JSONObject obj = (JSONObject) root.get(i);
            JSONObject item = (JSONObject) obj.get("order");
            JSONObject tb_order = (JSONObject) item.get("tb_order");
            JSONObject tb_express = obj.get("express").isObject();
            JSONValue tb_status = obj.get("status");

            OrderDO order = getOrder(tb_order);
            ExpressDO express = null;
            if (tb_express != null) {
                JSONValue expressItem = tb_express.get("express");
                if (expressItem != null) {
                    express = getExpress(expressItem.isObject());
                    order.setExpress(express);

                    if (tb_status != null) {
                        JSONArray list = tb_status.isArray();
                        if (list != null) {
                            List<StatusInfo> statuses = new ArrayList<StatusInfo>();
                            for (int j = 0; j < list.size(); j++) {
                                JSONValue value = list.get(j);
                                if (value != null && value.isObject() != null) {
                                    JSONValue statusValue = value.isObject().get("status");
                                    if (statusValue != null && statusValue.isObject() != null) {
                                        StatusInfo info = getStatus(statusValue.isObject());
                                        statuses.add(info);
                                    }
                                }
                            }
                            express.setStatuses(statuses);
                        }
                    }

                }
            }
            order.setExpress(express);
            order.getDetail();// 设置detail信息
            order.getLastStatus();// 获得状态
            order.setNickImage();
            records.add(order);
        }
        PagingLoadResult retVal = new BasePagingLoadResult(records);
        retVal.setTotalLength(count);
        retVal.setOffset(offset);
        return retVal;
    }

    private OrderDO getOrder(JSONObject object) {
        if (object == null) return null;
        OrderDO model = new OrderDO();
        JSONUtil.setModelValue(object, model, getOrderModelType());
        return model;
    }

    private ModelType getOrderModelType() {
        ModelType modelType = new ModelType();
        modelType.addField("price");
        modelType.addField("created_at");
        modelType.addField("title");
        modelType.addField("seller_rate");
        modelType.addField("updated_at");
        modelType.addField("tid");
        modelType.addField("iid");
        modelType.addField("buyer_nick");
        modelType.addField("seller_nick");
        modelType.addField("id");
        modelType.addField("buyer_rate");
        modelType.addField("user_id");
        modelType.addField("num");
        modelType.addField("status");
        modelType.addField("order_no");
        modelType.addField("order_created_at");
        modelType.addField("pic_path");
        return modelType;
    }

    private ExpressDO getExpress(JSONObject obj) {
        if (obj == null) return null;
        ExpressDO model = new ExpressDO();
        JSONUtil.setModelValue(obj, model, getExpressModelType());

        return model;
    }

    private ModelType getExpressModelType() {
        ModelType modelType = new ModelType();
        modelType.addField("no_code");
        modelType.addField("is_new");
        modelType.addField("created_at");
        modelType.addField("updated_at");
        modelType.addField("status_updated_at");
        modelType.addField("is_sync");
        modelType.addField("id");
        modelType.addField("is_end");
        modelType.addField("is_old");
        modelType.addField("not_exist");
        modelType.addField("agent_id");
        return modelType;
    }

    private StatusInfo getStatus(JSONObject object) {
        if (object == null) return null;
        StatusInfo model = new StatusInfo();
        JSONUtil.setModelValue(object, model, getStatusModelType());
        return model;
    }

    private ModelType getStatusModelType() {
        ModelType modelType = new ModelType();
        modelType.addField("is_new");
        modelType.addField("no");
        modelType.addField("created_at");
        modelType.addField("updated_at");
        modelType.addField("info");
        modelType.addField("id");
        modelType.addField("hash_code");
        modelType.addField("happen_time");
        modelType.addField("agent_id");
        return modelType;
    }

}
